package com.xiaohe;

import com.xiaohe.dao.UserDao;
import com.xiaohe.pojo.User;
import com.xiaohe.service.UserService;
import com.xiaohe.service.impl.UserServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 功能描述
 *
 * @author 和振斌
 * @date 2021/1/3
 * @return
 */
public class TestDemo {

    @Test
    public void testSpring(){
     //获取spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserServiceImpl userService = context.getBean("userService", UserServiceImpl.class);
        userService.findAll();
    }

    @Test
    public void testMybatis() throws IOException {
        //加载配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> user = userDao.findAll();
        for (User user1 : user) {
            System.out.println(user1);
        }
        sqlSession.close();
    }
}
